package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.domain.vo.PointsConfigDetailVo;
import com.ruoyi.system.domain.vo.PointsConfigVo;
import com.ruoyi.system.domain.vo.UserPointsListVo;
import com.ruoyi.system.dto.*;
import com.ruoyi.system.mapper.*;
import com.ruoyi.system.service.IOrderService;
import com.ruoyi.system.service.IPointService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PointServiceImpl implements IPointService {
    @Autowired
    private PointsConfigMapper pointsConfigMapper;
    @Autowired
    private PointsConfigDetailMapper pointsConfigDetailMapper;
    @Autowired
    private UserPointsMapper userPointsMapper;
    @Autowired
    private UserPointsLogMapper userPointsLogMapper;
    @Override
    public List<PointsConfigDTO> getList(String name) {
        LambdaQueryWrapper<PointsConfig> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(name)){
            wrapper.like(PointsConfig::getPackageName,name);
        }
        List<PointsConfig> pointsConfigs = pointsConfigMapper.selectList(wrapper);
        List<PointsConfigDTO> res = new ArrayList<>();
        // 将pointsConfigs的数据转换为PointsConfigDTO
        for (PointsConfig config : pointsConfigs) {
            PointsConfigDTO dto = new PointsConfigDTO();
            BeanUtils.copyProperties(config,dto);
            res.add(dto);
        }
        return res;
    }

    @Override
    public Boolean addOrUpdate(PointsConfigVo pointsConfigVo) {
        List<PointsConfigDetailVo> details = pointsConfigVo.getDetails();
        Long id = pointsConfigVo.getId();
        if (pointsConfigVo.getId() != null){
            // 修改
            PointsConfig pointsConfig = new PointsConfig();
            BeanUtils.copyProperties(pointsConfigVo,pointsConfig);
            pointsConfigMapper.updateById(pointsConfig);
            // 先删除详情
            LambdaQueryWrapper<PointsConfigDetail> detailWrapper = new LambdaQueryWrapper<>();
            detailWrapper.eq(PointsConfigDetail::getConfigId,pointsConfigVo.getId());
            pointsConfigDetailMapper.delete(detailWrapper);
        }else {
            // 新增
            PointsConfig pointsConfig = new PointsConfig();
            BeanUtils.copyProperties(pointsConfigVo,pointsConfig);
            pointsConfigMapper.insert(pointsConfig);
            id = pointsConfig.getId();
        }
        for (PointsConfigDetailVo detailDTO : details) {
            PointsConfigDetail pointsConfigDetail = new PointsConfigDetail();
            BeanUtils.copyProperties(detailDTO,pointsConfigDetail);
            pointsConfigDetail.setConfigId(id);
            pointsConfigDetailMapper.insert(pointsConfigDetail);
        }
        return true;
    }

    @Override
    public int countList(String name) {
        LambdaQueryWrapper<PointsConfig> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(name)){
            wrapper.like(PointsConfig::getPackageName,name);
        }
        return Math.toIntExact(pointsConfigMapper.selectCount(wrapper));
    }

    @Override
    public PointsConfigInfoDTO getInfo(Long id) {
        PointsConfigInfoDTO pointsConfigInfoDTO = new PointsConfigInfoDTO();
        LambdaQueryWrapper<PointsConfigDetail> detailWrapper = new LambdaQueryWrapper<>();
        detailWrapper.eq(PointsConfigDetail::getConfigId,id);
        PointsConfig pointsConfig = pointsConfigMapper.selectById(id);
        List<PointsConfigDetail> pointsConfigDetails = pointsConfigDetailMapper.selectList(detailWrapper);
        BeanUtils.copyProperties(pointsConfig,pointsConfigInfoDTO);
        List<PointsConfigDetailDTO> details = new ArrayList<>();
        for (PointsConfigDetail pointsConfigDetail : pointsConfigDetails) {
            PointsConfigDetailDTO pointsConfigDetailDTO = new PointsConfigDetailDTO();
            BeanUtils.copyProperties(pointsConfigDetail,pointsConfigDetailDTO);
            details.add(pointsConfigDetailDTO);
        }
        pointsConfigInfoDTO.setDetails(details);
        return pointsConfigInfoDTO;
    }

    @Override
    public List<UserPointsLogDTO> getUserPointsLogList(UserPointsListVo userPointsListVo) {
        LambdaQueryWrapper<UserPointsLog> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(userPointsListVo.getKeyword())){
            wrapper.and(w -> w.like(UserPointsLog::getUserName, userPointsListVo.getKeyword())
                .or()
                .like(UserPointsLog::getMobile, userPointsListVo.getKeyword()));
        }
        wrapper.eq(userPointsListVo.getType() != null, UserPointsLog::getSourceType, userPointsListVo.getType());
        wrapper.eq(userPointsListVo.getOperateType() != null, UserPointsLog::getOperateType, userPointsListVo.getOperateType());
        List<UserPointsLog> userPointsLog = userPointsLogMapper.selectList(wrapper);
        List<UserPointsLogDTO> res = new ArrayList<>();
        for (UserPointsLog upLog : userPointsLog) {
            UserPointsLogDTO dto = new UserPointsLogDTO();
            BeanUtils.copyProperties(upLog,dto);
            res.add(dto);
        }
        return res;
    }

    @Override
    public int getUserPointsLogCount(UserPointsListVo userPointsListVo) {
        LambdaQueryWrapper<UserPointsLog> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(userPointsListVo.getKeyword())){
            wrapper.and(w -> w.like(UserPointsLog::getUserName, userPointsListVo.getKeyword())
                .or()
                .like(UserPointsLog::getMobile, userPointsListVo.getKeyword()));
        }
        wrapper.eq(userPointsListVo.getType() != null, UserPointsLog::getSourceType, userPointsListVo.getType());
        wrapper.eq(userPointsListVo.getOperateType() != null, UserPointsLog::getOperateType, userPointsListVo.getOperateType());
        return Math.toIntExact(userPointsLogMapper.selectCount(wrapper));
    }

    @Override
    public List<UserPointsDTO> getUserPointsList(UserPointsListVo userPointsListVo) {
        LambdaQueryWrapper<UserPoints> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(userPointsListVo.getKeyword())){
            wrapper.and(w -> w.like(UserPoints::getUserName, userPointsListVo.getKeyword())
                .or()
                .like(UserPoints::getMobile, userPointsListVo.getKeyword()));
        }
        List<UserPoints> userPoints = userPointsMapper.selectList(wrapper);
        List<UserPointsDTO> res = new ArrayList<>();
        for (UserPoints up : userPoints) {
            UserPointsDTO dto = new UserPointsDTO();
            BeanUtils.copyProperties(up,dto);
            res.add(dto);
        }
        return res;
    }

    @Override
    public int getUserPointsCount(UserPointsListVo userPointsListVo) {
        LambdaQueryWrapper<UserPoints> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(userPointsListVo.getKeyword())){
            wrapper.and(w -> w.like(UserPoints::getUserName, userPointsListVo.getKeyword())
                .or()
                .like(UserPoints::getMobile, userPointsListVo.getKeyword()));
        }
        return Math.toIntExact(userPointsMapper.selectCount(wrapper));
    }
}
